package com.dundun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dundun.common.utils.Ajax;
import com.dundun.domain.Resume;
import com.dundun.mapper.ResumeMapper;
import com.dundun.service.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Paths;
import java.util.List;

@Api(tags = "简历相关接口")
@RequestMapping("/")
@RestController
@Configuration
public class ResumeController {
    @Resource
    private ResumeMapper resumeMapper;
    @Resource
    private ResumeService resumeService;

    // 用户空间下的地址
    private final String saveFilePath = System.getProperty("user.dir") + File.separator + "files";

    @ApiOperation(value = "提交简历到服务器")
    @PostMapping("/PostResume")
    public Ajax PostResume(@RequestParam("file") MultipartFile multipartFile, int userId,String fileName) {
        System.out.println(userId);
        System.out.println(fileName);
        if (userId == 0){
            return Ajax.error();
        }
        if (multipartFile.isEmpty()) {
            System.out.println("空文件");
            return Ajax.error();
        } else {
            switch (resumeService.addResume(multipartFile,userId,fileName)){
                case 0:
                    return Ajax.error().put("info", "文件已存在");
                case 1:
                    return Ajax.success().put("data","文件上传成功");
                default:
                    return Ajax.error();
            }
        }
    }

    @ApiOperation(value = "获取用户简历列表")
    @GetMapping("/GetResumeList")
    public Ajax GetResumeList(int userId) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Resume> list = resumeMapper.selectList(queryWrapper);
        return Ajax.success().put("data", list);
    }

    @ApiOperation(value = "分页获取用户简历列表")
    @GetMapping("/GetResumeListByPage")
    public Ajax GetResumeListByPage(int userId) {
        return Ajax.success().put("data", "the return data...");
    }

    @ApiOperation(value = "获取指定简历文件")
    @GetMapping("/GetResumeDetail")
    public void GetResumeDetail(int resumeId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("------------------------");
        Resume resume = resumeMapper.selectById(resumeId);
        String fileName = resume.getResumeName(); // 获取文件名称
        downloadFiles(fileName, response);
    }

    @ApiOperation(value = "删除指定简历文件")
    @GetMapping("/DeleteResume")
    public Ajax DeleteResume(int resumeId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        return Ajax.success().put("data", "the return data...");
    }

    @ApiOperation(value = "请求文件下载")
//    @GetMapping("{filename}")
    public void downloadFile(@PathVariable String filename, HttpServletResponse response) throws IOException {
        downloadFiles(filename, response);
    }


    //    下载方法
    private void downloadFiles(@PathVariable String filename, HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));

        final WritableByteChannel writableBatyChannel = Channels.newChannel(response.getOutputStream());
        final FileChannel fileChannel = new FileInputStream(Paths.get(saveFilePath + File.separator + filename).toFile()).getChannel();
        fileChannel.transferTo(0, fileChannel.size(), writableBatyChannel);

        fileChannel.close();
        writableBatyChannel.close();
    }

}
